package com.sfjswl.fp.core.context;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RuleRoam extends ConcurrentHashMap<String, Object> {
  public RuleRoam() {
  }
  public RuleRoam(int initialCapacity, float loadFactor) {
    super(initialCapacity, loadFactor);
  }

  public RuleRoam(int initialCapacity) {
    super(initialCapacity);
  }


  public RuleRoam(Map<String, Object> map) {
    super(map);
  }

  @SuppressWarnings("unchecked")
  public <T> T getValue(String key) {
    return (T) get(key);
  }

  public <T> T getValue(String key, T defaultValue) {
    T res = getValue(key);
    return res == null ? defaultValue : res;
  }

  @Override
  public Object put(String key, Object value) {
    if (key == null || value == null) {
      return null;
    }
    return super.put(key, value);
  }

  @Override
  public Object get(Object key) {
    if (key == null) {
      return null;
    }
    return super.get(key);
  }

  public <T> T getUnion(Object union) {
    if (union == null) {
      return null;
    }
    if (union instanceof String) {
      String key = (String) union;
      if (!(key).isEmpty() && (key).charAt(0) == '@') {
        return getUnion(get(key.substring(1)));
      }
      return (T) union;
    }
    return (T) union;
  }
}